<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../node_modules/vue/dist/vue.global.js"></script>
</head>

<body>
    <div id="app">
        <!-- 定义表单- 发表评论 -->
        <!-- <p>
            <input type="text" name="" id="" placeholder="评论人" v-model="publishman">
            <input type="text" name="" id="" placeholder="评论内容" v-model="publishCont">
            <button @click="publishFn">发表</button>
        </p> -->

        <part-child1 :data-arr='dataArr'></part-child1>
        <part-child2 :data-arr='dataArr'></part-child2>
        <!-- <ul>
            <li v-for="item in dataArr" :key='item.id'>
                <h4>{{item.publishman}}</h4>
                <p>{{item.publishCont}}</p>
            </li>
        </ul> -->
    </div>
</body>

</html>
<script>

    const app = Vue.createApp({
        data() {
            return {
                dataArr: [
                    {
                        id: 1,
                        publishman: '张三',
                        publishCont: '1111'
                    },
                    {
                        id: 2,
                        publishman: '李四',
                        publishCont: '2222'
                    }
                ]
            }
        },
        methods: {

        },
        components: {
            partChild1: {
                template: `<p>
                                <input type="text" name="" id="" placeholder="评论人" v-model="publishman">
                                <input type="text" name="" id="" placeholder="评论内容" v-model="publishCont">
                                <button @click="publishFn">发表</button>
                           </p>`,
                data() {
                    return {
                        publishman: '',  // 品论人
                        publishCont: '',  // 评论内容
                    }
                },
                props: ['dataArr'],
                methods: {
                    publishFn() {
                        // console.log(this);
                        this.dataArr.unshift({
                            id: this.dataArr[0].id - 1,
                            publishman: this.publishman,
                            publishCont: this.publishCont
                        })
                    }
                }
            },
            partChild2: {
                template: ` <ul>
                                <li v-for="item in dataArr" :key='item.id'>
                                    <h4>{{item.publishman}}</h4>
                                    <p>{{item.publishCont}}</p>
                                </li>
                            </ul>`,
                props: ['dataArr']
            }
        }
    }).mount('#app')

</script>